//
//  YsNoteSDKInit.m
//  YsNoteSDK
//
//  Created by 宋宏康 on 2020/3/21.
//

#import "YsNoteSDKInit.h"
#import "YsNoteSDKUserInforModel.h"
#import "YsNoteSDKHeader.h"
#import "YsNoteSDKViewModel.h"
#import <YsRealmTools/YsRealmTools.h>
@implementation YsNoteSDKInit

static YsNoteSDKInit *noteSDKintManager = nil;


+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        noteSDKintManager = [[self alloc] init];
        [noteSDKintManager initRealmData];
    });
    return noteSDKintManager;
}

- (void)noteSDKinitWithAccountCode:(NSString *)accountCode withServerDic:(NSDictionary<NSString *,NSString *> *)serverDic
               withInitStatusBlock:(void (^)(BOOL))initStatusBlock
{
    NSParameterAssert(accountCode);
    
    YsNoteSDKUserInforModel.shareManager.accountCode = accountCode;
    YsNoteSDKUserInforModel.shareManager.schoolCloudHost = [serverDic valueForKey:@"schoolCloudHost"];
    YsNoteSDKUserInforModel.shareManager.cloudSpaceHost = [serverDic valueForKey:@"cloudSpaceHost"];
    YsNoteSDKUserInforModel.shareManager.uploadServiceHost = [serverDic valueForKey:@"uploadServiceHost"];
    [self initFetchWithInitStatusBlock:initStatusBlock];
}

- (void)initFetchWithInitStatusBlock:(void (^)(BOOL))initStatusBlock
{
    [[YsNoteSDKViewModel getUploadFetchWayWithuid:kaccountCode] subscribeNext:^(id  _Nullable x) {
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"check"];
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"uploadFile"];
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"moveFile"];
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"fileStatus"];
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"freeWay"];
        YsNoteSDKUserInforModel.shareManager.check = [x objectForKey:@"forceDelete"];
    } error:^(NSError * _Nullable error) {
        if (initStatusBlock) initStatusBlock(NO);
    } completed:^{
        if (initStatusBlock) initStatusBlock(YES);
    }];
}

- (void)initRealmData
{
    /// 初始化数据库
    [YsRealmToolsManager setupConfigWithDirName:@"tmpRealm" withRealmName:@"SmartPen.realm"];
}
@end
